package initPackage;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

import database.ConnectionHandler;

public class AddCandidate
{
    public static ConnectionHandler connH = new ConnectionHandler();

    public static void main( String[] args )
    {
        Scanner sc = new Scanner( System.in );
        while( true )
        {
            System.out.println( "Add candidate" );
            System.out.println( "First name: " );
            String first = sc.nextLine();
            System.out.println( "Last name: " );
            String second = sc.nextLine();
            addCandidate( first, second );
            System.out.println( "-----------" );
        }
    }

    public static void addCandidate( String first, String second )
    {
        Connection c = connH.getConnection();
        try
        {
            Statement st2 = c.createStatement();
            ResultSet rs = st2.executeQuery( "SELECT max(id) from candidate" );
            rs.next();
            int maxId = rs.getInt( 1 );
            maxId++;
            Statement st = c.createStatement();
            st.execute( "insert into candidate(firstname, lastname, numberofvotes, id) values('" + first + "', '" + second + "', '0', '"
                    + maxId + "')" );
        }
        catch( Exception e )
        {
            System.out.println( e );
        }
    }
}
